package com.qzdsoft.erpcloud.controller.pu;

import java.util.List;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.erpcloud.controller.BaseController;
import com.qzdsoft.erpcloud.service.com.CustomerSupplierService;
import com.qzdsoft.erpcloud.service.com.CustomerSupplierStatementService;
import com.qzdsoft.erpcloud.service.proj.MaterialService;
import com.qzdsoft.erpcloud.service.pu.PurchaseInquiryDetailService;
import com.qzdsoft.erpcloud.service.pu.PurchaseInquiryService;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysLog;
import com.qzdsoft.erpcloud.service.sys.TablePermission;
import com.qzdsoft.erpcloud.vo.com.CustomerSupplierStatementInfo;
import com.qzdsoft.erpcloud.vo.proj.MaterialInfo;
import com.qzdsoft.erpcloud.vo.proj.MaterialQueryInfo;
import com.qzdsoft.erpcloud.vo.pu.PurchaseInquiryDetailEditInfo;
import com.qzdsoft.erpcloud.vo.pu.PurchaseInquiryDetailInfo;
import com.qzdsoft.erpcloud.vo.pu.PurchaseInquiryDetailQueryInfo;
import com.qzdsoft.erpcloud.vo.pu.PurchaseInquiryDetailRemoveInfo;
import com.qzdsoft.erpcloud.vo.pu.PurchaseInquiryInfo;
import com.qzdsoft.utils.ExceptionUtil;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;
import com.qzdsoft.vo.TypeInfo;
import com.qzdsoft.vo.proj.MaterialReviewStatus;
import com.qzdsoft.vo.pu.PurchaseInquiryEffectialType;

/***
 * 
 * 采购询价单详情controller
 *
 * <p>detailed comment
 * @author  2017年9月04日
 * @see
 * @since 1.0
 */
@Controller
@RequestMapping("/pu/purchaseinquiry")
public class PurchaseInquiryDetailController extends BaseController
{

    private static final Logger logger = org.slf4j.LoggerFactory.getLogger(PurchaseInquiryDetailController.class);
    
    private static final String BASEPATH = "pu/purchaseinquiry";

    @Autowired
    private PurchaseInquiryDetailService purchaseInquiryDetailService;
    
    @Autowired
    private PurchaseInquiryService purchaseInquiryService;

    @Autowired
    private MaterialService materialService;
    
    @Autowired
    private CustomerSupplierStatementService customerSupplierStatementService;
    
    @Autowired
    private CustomerSupplierService customerSupplierService;

    @TablePermission(id="16")
    @Permission(validate=false)
    @RequestMapping("/detail")
    public ModelAndView index(Integer id) {
        //查询采购询价单
        PurchaseInquiryInfo purchaseInquiryInfo = purchaseInquiryService.findPurchaseInquiryBy(id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("purchaseInquiry",purchaseInquiryInfo);
        mv.setViewName(BASEPATH + "/detail");
        return mv;
    }
    /**
     * 跳转到页面
     * @return
     */
    @RequestMapping("/order-details")
    public ModelAndView item(Integer id,PurchaseInquiryDetailQueryInfo queryinfo)
    {
        //查询物料信息
        MaterialQueryInfo info = new MaterialQueryInfo();
        info.setIsBuy(true);//采购的物料
        info.setStatus(MaterialReviewStatus.HAS_REVIEW_CODE);//查询已经审核的物料
        List<MaterialInfo> materialInfos = materialService.selectBy(info);
        ModelAndView mv = new ModelAndView();
        mv.addObject("materials", materialInfos);
        
        //查询采购询价单
        PurchaseInquiryInfo purchaseInquiryInfo = purchaseInquiryService.findPurchaseInquiryBy(id);
        mv.addObject("purchaseInquiry",purchaseInquiryInfo);
        
        Integer companyid = purchaseInquiryInfo.getCompanyid();
        Integer supplierid= purchaseInquiryInfo.getSupplierid();
        
        //供应商结算方式
        List<CustomerSupplierStatementInfo> customerSupplierStatements = customerSupplierStatementService.selectBy(companyid, supplierid);
        mv.addObject("statements",customerSupplierStatements);
        
        //生效规则
        List<TypeInfo> effectialType = PurchaseInquiryEffectialType.ALL;
        mv.addObject("effectialType",effectialType);
        
        //税率
        //TypeInfo taxInfo = customerSupplierService.selectTaxInfo(companyid, supplierid);
        //mv.addObject("taxInfo",taxInfo);
        
        //明细列表
        List<PurchaseInquiryDetailInfo> list = purchaseInquiryDetailService.selectDetailBy(queryinfo);
        mv.addObject("details",list);
        
        mv.setViewName(BASEPATH + "/detailItem");
        return mv;
    }
    
    /**
     * 跳转到页面
     * @return
     */
    @SysLog(description="编辑采购询价单明细")
    @ResponseBody
    @RequestMapping("/edit-orderdetail")
    public Response<String> edit(@RequestBody PurchaseInquiryDetailEditInfo info) {
       
        try
        {
            return purchaseInquiryDetailService.edit(info);
        }
        catch (Exception ex)
        {
            logger.error("编辑采购询价单明细错误，错误信息：{}", ExceptionUtil.getStackTrace(ex));
        }
        return new Response<String>(ResultEnum.ERROR);
    }
    
    @SysLog(description="采购询价单详情删除")
    @ResponseBody
    @RequestMapping("/details/remove")
    public Response<String> remove(@RequestBody PurchaseInquiryDetailRemoveInfo info){
        try
        {
            logger.debug("采购询价单详情删除，ids：{}",info.getIds());
            return purchaseInquiryDetailService.remove(info.getIds());
        }
        catch (Exception ex)
        {
            logger.error("编辑采购询价单明细错误，错误信息：{}", ExceptionUtil.getStackTrace(ex));
        }
        return new Response<String>(ResultEnum.ERROR);
    }
    
    /**
     * 查询物料信息
     * @param info
     * @return
     */
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/materias")
    public LayTableResponse<MaterialInfo> list(MaterialQueryInfo info){
        info.setIsBuy(true);
        info.setStatus(MaterialReviewStatus.HAS_REVIEW_CODE);//查询已经审核的物料
        return materialService.selectPageBy(info);
    }

    /**
     * 物料选择页面
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/selectmaterials")
    public ModelAndView selectMaterial() {
        ModelAndView view = new ModelAndView();
        view.setViewName(BASEPATH+"/materials");
        return view;
    }
    
 /*   //**
     * 返回所有之前的采购询价单单详情
     * @param id
     * @return
     *//*
    @ResponseBody
    @RequestMapping("/list/{id}")
    public List<PurchaseInquiryDetailInfo> list(@PathVariable("id") int id)
    {
        return purchaseInquiryDetailService.selectDetailBy(id);
    }*/

}
